/* 辅助工具箱 */
var zycmUtils = {
    dateFormatYMDHMS: function(datalong) {
        // 时间戳转 yy-MM-dd hh:mm:ss格式的字符串
        if (datalong * 1 == 0) {
            return "";
        }
        /*  if((datalong+"").length < 13){//php时间戳是10位 java时间戳13位
             datalong = datalong*1000
         } */
        var date = new Date(datalong);
        var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
        var hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
        var min = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
        var sec = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
        return date.getFullYear() + "-" + month + "-" + day + " " + hour +
            ":" + min + ":" + sec;
    },
    dateFormatYMD: function(datalong) {
        // 时间戳转 yy-MM-dd hh:mm:ss格式的字符串
        if (datalong * 1 == 0) {
            return "";
        }
        /* if((datalong+"").length < 13){//php时间戳是10位 java时间戳13位
            datalong = datalong*1000
        } */
        var date = new Date(datalong);
        var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();

        return date.getFullYear() + "-" + month + "-" + day;
    },
    dateFormatHMS: function(datalong) {
        // 时间戳转 yy-MM-dd hh:mm:ss格式的字符串
        /* if(datalong*1 == 0){
            return "";
        } */
        /* if((datalong+"").length < 13){//php时间戳是10位 java时间戳13位
            datalong = datalong*1000
        } */
        var date = new Date(datalong);
        var hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
        var min = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
        var sec = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
        return hour + ":" + min + ":" + sec;
    },
    strToDate: function(s) {
        //字符类型时间yy-MM-dd hh:mm:ss格式  转成date类型
        if (!s) return new Date();
        var y = s.substring(0, 4);
        var m = s.substring(5, 7);
        var d = s.substring(8, 10);
        var h = s.substring(11, 13);
        var min = s.substring(14, 16);
        var sec = s.substring(17, 19);
        if (!isNaN(y) && !isNaN(m) && !isNaN(d) && !isNaN(h) && !isNaN(min) && !isNaN(sec)) {
            return new Date(y, m - 1, d, h, min, sec);
        } else {
            return new Date();
        }
    },
    checkName: function(val) {
        //验证名称不含非法字符
        if (!val) {
            return '名称不能为空';
        }
        if (val.indexOf(' ') >= 0) {
            return '名称不能有空字符';
        }
        if (/\<|\>|\/|\\|\||\:|\：|\"|\'|\*|\?|\？|\、|\,|\。|\，|\%|\#/.test(val)) {
            return '名称中出现< > /  \\ | : ： \" \' * ? ？\ 、,。， % #等非法字符';
        }
        if (val.length > 30) {
            return '名称长度不能超过30';
        }
    },
    numMulti: function(num1, num2) {
        //乘法运算，避免数据相乘小数点后产生多位数和计算精度损失。
        var baseNum = 0;
        try {
            baseNum += num1.toString().split(".")[1].length;
        } catch (e) {}
        try {
            baseNum += num2.toString().split(".")[1].length;
        } catch (e) {}
        return Number(num1.toString().replace(".", "")) *
            Number(num2.toString().replace(".", "")) /
            Math.pow(10, baseNum);
    },
    convertBytes: function(limit) {
        //转换文件大小
        if (limit == null) {
            return "";
        }
        var size = "";
        if (limit < 1024.00) { //小于1KB，则转化成B
            size = limit + "B"
        } else if (limit < 1024 * 1024) { //小于1MB，则转化成KB
            size = (limit / 1024.00).toFixed(2) + "KB"
        } else if (limit < 1024 * 1024 * 1024) { //小于1GB，则转化成MB
            size = (limit / (1024.00 * 1024)).toFixed(2) + "MB"
        } else { //其他转化成GB
            size = (limit / (1024.00 * 1024 * 1024)).toFixed(2) + "GB"
        }
        return size;
    },
    convertSecond: function(seconds) {
        //时间差值转换
        var re = 0;
        if (seconds != null && seconds != undefined && seconds != "") {
            var sen = seconds * 1;
            if (sen > 86400) { //天
                re = (sen / 86400).toFixed(2) + " 天";
            } else if (sen > 3600) { //大于3600 转小时
                re = (sen / 3600).toFixed(2) + " 小时";
            } else if (sen > 60) { //大于60 转分钟
                re = (sen / 60).toFixed(2) + " 分钟";
            } else {
                re = sen + " 秒";
            }
        }
        return re;
    },
    checkCNWord(wd) {
        let reg = /[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/;
        if (reg.test(wd)) {
            //是中文标点符号
            return true;
        } else {
            //不是中文标点符号
            return false;
        }
    },
    checkCN(wd) {
        let reg = /^[\u4E00-\u9FA5]+$/;
        if (reg.test(wd)) {
            //是中文
            return true;
        } else {
            //不是中文
            return false;
        }
    },
    getLableByVal(val, arr) {
        for (let index in arr) {
            if (arr[index].value === val) {
                return arr[index].label;
            }
        }
        return "";
    },
    getValByLabel(label, arr) {
        for (let index in arr) {
            if (arr[index].label === label) {
                return arr[index].value;
            }
        }
        return "";
    },
    calculationWH(l_width,l_height,c_w,c_h){//根据容器的宽高及存放容器的目标的宽高  计算目标等比缩放后最适合的尺寸
       	//l_width,l_height为目标实际宽高    c_w,c_h为容器的宽高
        l_width = l_width*1;
        l_height = l_height*1;
        let p=1;
        let max_w,max_h;
        //先判断是否需要计算压缩值
        if(l_width <= c_w && l_height <= c_h){
            //无需压缩
            max_w = l_width;
            max_h = l_height;
            p=1;
        }else{
            //计算最合适的压缩值
            if(l_height >= l_width){
                //竖屏、正方的屏
                //先用目标高度计算与容器高度的最合适的压缩值
                p = this.formatDecimal(c_h/l_height,2);
                //再判断压缩值是否满足容器的宽度
                max_w = this.numMulti(l_width,p);
                //判断目标的宽压缩后是否会大于容器的宽  如果小于则压缩值是合适的直接返回  如果大于说明容器放不下目标的宽度还需进一步计算压缩值
                if(max_w > c_w){
                    //进一步计算压缩值  以宽度来计算
                    p = this.formatDecimal(c_w/l_width,2);
                    max_h = this.numMulti(l_height,p);
                    if(max_h > c_h){
                        layer.alert(l_width+"*"+l_height+"该尺寸节目系统暂不支持制作！");
                        return null;
                    }
                }
            }else if(l_height < l_width){
                //横屏
                //先用目标宽度计算与容器宽度的最合适的压缩值
                p = this.formatDecimal(c_w/l_width,2);
                max_h = this.numMulti(l_height,p);
                if(max_h > c_h){
                    //进一步计算压缩值  以高度来计算
                    p = this.formatDecimal(c_h/l_height,2);
                    max_w = this.numMulti(l_width,p);
                    if(max_w > c_w){
                        layer.alert(l_width+"*"+l_height+"该尺寸节目系统暂不支持制作！");
                        return null;
                    }
                }
            }

        }

        return {'max_w':this.numMulti(l_width,p),'max_h':this.numMulti(l_height,p),'multiple':p};
    },
    formatDecimal:function (num1, decimal1) {
        num1 = num1.toString();
        let index = num1.indexOf('.');
        if (index !== -1) {
            num1 = num1.substring(0, decimal1 + index + 1);
        } else {
            num1 = num1.substring(0);
        }
        return parseFloat(num1).toFixed(decimal1);
    },
    convertAiAuditLabel:function(label){//转换智能审核返回结果为字符串
        let re = "智能审核处理中";
        switch (label) {
            case "normal":
                re = "正常";
                break;
            case "exception":
                re = "审核异常";
                break;
            case "checking":
                re = "视频审核较慢,请关闭窗口稍后点开尝试，或直接人工审核";
                break;
            case "noresult":
                re = "无结果返回";
                break;
            case "sexy":
                re = "性感";
                break;
            case "porn":
                re = "色情";
                break;
            case "bloody":
                re = "血腥";
                break;
            case "explosion":
                re = "爆炸烟光";
                break;
            case "outfit":
                re = "特殊装束";
                break;
            case "logo":
                re = "特殊标识";
                break;
            case "weapon":
                re = "武器";
                break;
            case "politics":
                re = "涉政";
                break;
            case "violence":
                re = "打斗";
                break;
            case "crowd":
                re = "聚众";
                break;
            case "parade":
                re = "游行";
                break;
            case "carcrash":
                re = "车祸现场";
                break;
            case "flag":
                re = "旗帜";
                break;
            case "location":
                re = "地标";
                break;
            case "others":
                re = "其他";
                break;
            case "ad":
                re = "其他广告";
                break;
            case "abuse":
                re = "含辱骂内容";
                break;
            case "terrorism":
                re = "含暴恐内容";
                break;
            case "contraband":
                re = "含违禁内容";
                break;
            case "spam":
                re = "含其他垃圾内容";
                break;
            case "npx":
                re = "牛皮癣广告";
                break;
            case "qrcode":
                re = "含二维码";
                break;
            case "programCode":
                re = "含小程序码";
                break;
            case "meaningless":
                re = "图片中无内容";
                break;
            case "PIP":
                re = "画中画";
                break;
            case "smoking":
                re = "吸烟";
                break;
            case "drivelive":
                re = "车内直播";
                break;
            case "TV":
                re = "含受管控的logo";
                break;
            case "trademark":
                re = "含商标";
                break;
            case "live":
                re = "包含不良场景";
                break;
            case "flood":
                re = "灌水";
                break;
            case "customized":
                re = "自定义关键词违规";
                break;
            default:
                re = "未知状态";
        }
        return re;
    },
    dateTimeArea(day){
        var today = new Date();
        let start = today.getFullYear()+"-"+this.doHandleMonth(today.getMonth() + 1)+"-"+this.doHandleMonth(today.getDate());
    　　var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
    　　today.setTime(targetday_milliseconds); //注意，这行是关键代码
    　　var tYear = today.getFullYear();
    　　var tMonth = today.getMonth();
    　　var tDate = today.getDate();
    　　tMonth = this.doHandleMonth(tMonth + 1);
    　　tDate = this.doHandleMonth(tDate);
        let end = tYear+"-"+tMonth+"-"+tDate;
        if(day < 0){
            return [end+" 00:00:00",start+" 23:59:59"];
        }
    　　return [start+" 00:00:00",end+" 23:59:59"];
    },
    doHandleMonth(month){
    　　var m = month;
    　　if(month.toString().length == 1){
    　　　　m = "0" + month;
    　　}
    　　return m;
    }

}
export default zycmUtils
